//
//  STAccountInfoView.swift
//  iPhone
//
//  Created by Toj on 8/9/22.
//

import UIKit

private let imageViewWH: CGFloat = 36
private let vipIconWidth: CGFloat = 40
private let vipIconMargin: CGFloat = 2

protocol STAccountInfoViewDelegate: NSObjectProtocol {
    func accountInfoViewDidTapSignin(_ view: STAccountInfoView)
    func accountInfoViewDidTapEditorInfo(_ view: STAccountInfoView)
}

extension STAccountInfoViewDelegate {
    func accountInfoViewDidTapSignin(_ view: STAccountInfoView) { }
    func accountInfoViewDidTapEditorInfo(_ view: STAccountInfoView) { }
}

class STAccountInfoView: UIView {
    
    var delegate: STAccountInfoViewDelegate?
    
    var model: NSObject? {
        didSet {
           // guard let info = model else { return }
            
            nicknameLabel.text = "NormanNormanNormanNormanNormanNormanNormanNorman"
            affiliatedLabel.text = "12:20 Jul.16 2022;"
            headImageView.image = "commodity_head_default".to_image
            setupRelayoutSubviews()
            arrowsImageView.isHidden = !isArrows
             
            affiliatedLabel.textColor = nicknameLabel.textColor
            
            let islogin = arc4random_uniform(20) > 0
            signinButton.isHidden = islogin
            infoView.isHidden = !islogin
        }
    }
    
    var isArrows = false
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        setupMakeInitialize()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
    }
    
    private lazy var headImageView: UIImageView = {
        let view = UIImageView()
        view.cm_cornerRadius = imageViewWH * 0.5
        view.cm_border(color: "#E8CD8C".to_color)
        return view
    }()
    
    private lazy var infoView: UIView = UIView()
    
    private lazy var nicknameLabel: UILabel = {
        let view = UILabel()
        view.textColor = UIColor(hex:"#E8CD8C")
        view.font = UIFont.boldSystemFont(ofSize: 16.f_scale)
        return view
    }()
    
    private lazy var vipIcon: UIImageView = {
        let view = UIImageView()
        view.image = "product_info_vip_icon_sel".to_image
        return view
    }()
    
    private lazy var affiliatedLabel: UILabel = {
        let view = UILabel()
        view.numberOfLines = 0
        view.font = UIFont.systemFont(ofSize: 14.f_scale)
        return view
    }()
    
    private lazy var arrowsImageView: UIImageView = {
        let view = UIImageView()
        view.image = "me_info_arrows_right_white".to_image
        return view
    }()
    
    private lazy var signinButton: UIButton = {
        let view = UIButton()
        view.isHidden = true
        view.backgroundColor = UIColor.blue
        view.adjustsImageWhenDisabled = false
        view.adjustsImageWhenHighlighted = false
        view.setTitle("Sign in", for:.normal)
        view.setTitleColor(UIColor.white, for: .normal)
        view.addTarget(self, action:#selector(didTapSignin), for:.touchUpInside)
        return view
    }()
    
    deinit {
        // print("-->toj: CommodityHeaderView: deinit")
    }
}

extension STAccountInfoView {
    
    @objc private func didTapSignin() {
        
    }
}

// MARK: - 初始化
extension STAccountInfoView {
    
    private func setupMakeAddSubviews() {
        
        addSubview(headImageView)
        addSubview(infoView)
        addSubview(signinButton)
        
        infoView.addSubview(nicknameLabel)
        infoView.addSubview(vipIcon)
        infoView.addSubview(affiliatedLabel)
        infoView.addSubview(arrowsImageView)
    }
    
    private func setupMakeLayoutSubviews() {
        
        headImageView.snp.makeConstraints { make in
            make.centerY.equalTo(self)
            make.left.equalTo(16)
            make.width.equalTo(imageViewWH)
            make.height.equalTo(imageViewWH)
        }
        
        signinButton.snp.makeConstraints { (make ) in
            make.left.equalTo(headImageView.snp.right).offset(20)
            make.centerY.equalTo(headImageView)
            make.height.equalTo(30)
            make.width.equalTo(80)
        }
        
        arrowsImageView.snp.makeConstraints { make in
            make.centerY.equalTo(self)
            make.right.equalTo(snp.right).offset(-16)
            make.width.equalTo(25)
            make.height.equalTo(25)
        }
        
        setupRelayoutSubviews()
    }
    
    private func setupRelayoutSubviews() {
        
        let selfHeight = frame.height
        if selfHeight == 0 { return }
        
        let rightMargin: CGFloat = isArrows ? 50 : 16 // 16 + 25 + 9
        let leftMargin: CGFloat = 64
        
        var nicknameWidth = nicknameLabel.text?.cm_stringWidth(font: nicknameLabel.font, maxHeight: 10) ?? 0
        let nicknameHeight = "".cm_stringHeight(font: nicknameLabel.font, maxWidth: .sr_width)
        
        let affiliatedWidth = .sr_width - leftMargin - rightMargin
        
        let nicknameMaxWidth = affiliatedWidth - vipIconMargin - vipIconWidth
        if nicknameWidth > nicknameMaxWidth { nicknameWidth = nicknameMaxWidth }
        
        var affiliatedHeight = affiliatedLabel.text?.cm_stringHeight(font:  affiliatedLabel.font, maxWidth: affiliatedWidth) ?? 0
        let totalHeight = nicknameHeight + affiliatedHeight
        if totalHeight > selfHeight {
            affiliatedHeight = selfHeight - nicknameHeight
        }
        let top = (selfHeight - nicknameHeight - affiliatedHeight) * 0.5
        // print("-->toj: nicknameWidth:\(nicknameWidth)")
        nicknameLabel.snp.remakeConstraints { make in
            make.top.equalTo(top)
            make.left.equalTo(headImageView.snp.right).offset(12.ui_scale)
            make.width.equalTo(nicknameWidth)
        }
        
        vipIcon.snp.remakeConstraints { make in
            make.left.equalTo(nicknameLabel.snp.right).offset(vipIconMargin)
            make.centerY.equalTo(nicknameLabel)
            make.width.equalTo(vipIconWidth)
            make.height.equalTo(16)
        }
        
        affiliatedLabel.snp.remakeConstraints { make in
            make.width.equalTo(affiliatedWidth)
            make.height.equalTo(affiliatedHeight)
            make.left.equalTo(nicknameLabel)
            make.top.equalTo(nicknameLabel.snp.bottom)
        }
    }
    
    private func setupMakeInitialize() {
        //layoutIfNeeded()
    }
}
